#! /usr/bin/env/python3
# coding:UTF-8
'''
    名称：掷骰子
    作者：王楠
    版本: 4.0
    日期：2020/03/01
    功能：dice_num，几个骰子
         3.0: 数据可视化 散点图
         4.0: 直方图
'''
import random
import matplotlib.pyplot as plt
'''
font = {'family': 'monospace',
        'weight': 'bold',
        'size': '16'}
plt.rc('font', **font)               # 步骤一（设置字体的更多属性）
plt.rc('axes', unicode_minus=False)  # 步骤二（解决坐标轴负数的负号显示问题）
'''
plt.rcParams['font.sans-serif'] = ['SimSun']  # 步骤一（替换sans-serif字体）
plt.rcParams['axes.unicode_minus'] = False    # 步骤二（解决坐标轴负数的负号显示问题）


class Dice():
    def roll_dice(self):
        numeral = random.randint(1, 6)
        return numeral


def dice_main():
    '''自己写：
    nums = []
    roll_times = int(input('请输入投掷次数：'))
    dice_num = int(input('请输入骰子个数：'))
    result_list_times = [0] * (6 * dice_num - dice_num + 1)
    # result_list_numeral = list(range(dice_num, 6 * dice_num + 1))
    # result_dict = dict(zip(result_list_numeral, result_list_times))
    dice = Dice(dice_num)
    for i in range(roll_times):
        num = dice.roll_dice()
        nums.append(num)
        for x in range(dice_num, 6 * dice_num + 1):
            if num == x:
                result_list[x-dice_num] += 1
    print(nums)
    print('{}个骰子，投掷了{}次：'.format(dice_num, roll_times))
    for i, result in enumerate(result_list):
        print('{}出现了{}次,频率是{}%'.format(
            i + dice_num, result,
            result / roll_times * 100))
    '''
    result_sum_list = []      # 记录点数之和
    dice_case_list = []       # 记录骰子对象
    roll_times = int(input('请输入投掷次数：'))
    dice_num = int(input('请输入骰子个数：'))
    numeral_list = [[] for i in range(dice_num)]
    print(numeral_list)
    result_list_times = [0] * (6 * dice_num - dice_num + 1)
    result_list_numeral = list(range(dice_num, 6 * dice_num + 1))
    result_dict = dict(zip(result_list_numeral, result_list_times))
    for i in range(dice_num):
        dice_case_list.append(Dice())
    for i in range(roll_times):
        sum_of_dices = 0
        for n in range(dice_num):
            numeral_list[n].append(dice_case_list[n].roll_dice())
            sum_of_dices += numeral_list[n][i]
        for x in result_list_numeral:
            if sum_of_dices == x:
                result_dict[x] += 1
                result_sum_list.append(x)
    print(numeral_list)
    print(result_sum_list)
    print('{}个骰子，投掷了{}次：'.format(dice_num, roll_times))
    # for result_num in result_list_numeral:
    #     print('出现点数{0}的次数是{1}次,频率是{2:.1f}%'.format(
    #         result_num, result_dict[result_num],
    #         result_dict[result_num] / roll_times * 100))
    # print('{}个骰子，投掷了{}次：'.format(dice_num, roll_times))
    for result_num, result_times in result_dict.items():
        print('出现点数{0}的次数是{1}次,频率是{2:.1f}%'.format(
            result_num, result_times,
            result_times / roll_times * 100))
    # 数据可视化
    colors = ['red', 'b', 'white', 'green', '#fb320e']
    x_list = list(range(1, roll_times + 1))
    # 输出方式选择
    fig = plt.figure(num='掷骰子统计图', figsize=(10, 10))
    ax1 = fig.add_subplot(221)
    ax1.set_title('单个骰子散点分布图 ')    # 设置标题
    plt.xlabel('投掷次数')                # 设置X轴标签
    plt.ylabel('每个骰子骰子点数')                   # 设置Y轴标签
    plt.legend('x1')                    # 设置图标
    for i in range(dice_num):
        y_list = numeral_list[i]
        ax1.scatter(x_list, y_list, c=colors[i], alpha=0.5)
    ax2 = fig.add_subplot(223)
    ax2.set_title('点数和散点分布图 ')    # 设置标题
    plt.xlabel('投掷次数')                # 设置X轴标签
    plt.ylabel('骰子点数和')                   # 设置Y轴标签
    plt.legend('x2')                    # 设置图标
    x_ax1 = list(range(1, roll_times + 1))
    y_ax2 = result_sum_list
    ax2.scatter(x_ax1, y_ax2)
    ax3 = fig.add_subplot(222)
    ax3.set_title('点数和直方图')
    plt.xlabel('点数')
    plt.ylabel('出现的次数')
    ax3.hist(result_sum_list, bins=range(dice_num, 6 * dice_num + 2), edgecolor='black', linewidth=1)
    ax4 = fig.add_subplot(224)
    ax4.set_title('点数和直方图')
    plt.xlabel('点数')
    plt.ylabel('出现的频率')
    ax4.hist(result_sum_list, bins=range(dice_num, 6 * dice_num + 2), normed=1, edgecolor='black', linewidth=1)





    plt.show()


if __name__ == '__main__':
    dice_main()
